<?php
	function createView($view){		
		
	   $current_user = wp_get_current_user(); 
	   if($current_user->ID == 0){
	   		return '';
	   }
	  
	  	//echo json_encode($current_user->caps["administrator"]);	 

	   include_once "functions.php";
	   $user = UsersORM::getUser(true);
	  // echo json_encode($user);
	   $role_status = $user['role_status'];

	   $simple = true;
	   if($view->table == "custom"){
	   		$simple = false;
	   }	

	   global $wpdb;

	   //echo $view->prefix;
	   //echo $view->view_name;

	   //echo  'role ' . $view->role_status;

	   $res = $wpdb->get_var("SELECT COUNT(*) FROM {$view->prefix}{$view->view_name}");

	   //echo json_encode($res);
	   /*
	   echo json_encode($user);
	   echo json_encode($view->user);
	   echo "<br>";
	   */

	   //echo $view->findAllQuery();
	   //echo  $view->granted;
	   //echo json_encode($view);
	   
	   /*if($view->granted != 1){
	   		die();
	   }*/
	   
	   

?>

<style>
	.h3 {
		color:grey;
	}

</style>
	

	

	<?php if( $view->class =="clientcourseView"){ 
		?>
	<div >		
		<br><br><br><br><br><br>
	<div>
		<form  data-bind="submit:clickSearch">
			<div id="the-basics">
			  <input class="typeahead" type="text" placeholder="Клиент или Преподаватель"  data-bind="value:searchString">
			  <button type="button" name="Поиск" data-bind="click:clickReset">Сброс</button>
			  <button type="button" name="Поиск" data-bind="click:clickSearch">Поиск</button>
			</div>

			
		</form>
	</div>
	<?php } else { ?>
		<h3 class="h3 form_header" id="<?php echo $view->class; ?>_view_table_click"> <?php echo $view->name; ?> <span id="<?php echo $view->class; ?>_view_table_v"> >> </span> </h3>
		<div id="<?php echo $view->class; ?>_view_table" style="display: none;"> 
	 <?php  }	 ?>


	<table class="timeTable minpadding">
		<thead>
			<tr>
				<th class="center">N</th>
				<th class="edit" data-bind="click:views.<?php echo $view->class;?>().clickNew" colspan="3"><span class="glyphicon glyphicon-plus"></th>
				<?php 
					//echo json_encode($view->header);
					foreach ($view->header as $key => $value) {
						//echo json_encode($value);
						$count_td_main = 4;
						$count_add = 4;
						if($simple){$value["place"] = "main";};
					 	if($simple || $value["place"]=="main"){
					 		$count_td_main += 1;
				?>
					<th class="center minpadding" data-bind="click:views.<?php echo $view->class;?>().sort" name="<?php echo $value['key']['field']; ?>"> <?php echo $value["name"]; ?>  </th>
				<?php  
						}
					} ?>
				<?php if( $view->class =="clientcourseView"){  ?>	
						<th>Оплата</th>
				<?php } ?>		
			</tr>
			<?php if(!$simple){ ?>
			<tr class="addrow">
				<th colspan = "4"> </th>
				<?php 
					foreach ($view->header as $key => $value) {
						$count_td_main = 4;
						$count_add = 0;
						
					 	if($value["place"]=="add"){
					 		
				?>
					<th class="center minpadding" data-bind="click:parent.sortBody"  name="<?php echo $value['key']['field']; ?>"> <?php echo $value["name"]; ?>   </th>
				<?php  
						}
					} 
				?>

				<?php if($count_td_main - $count_add > 0){ ?>
					<td colspan="<?php echo ($count_td_main - $count_add); ?>"></td>
				<?php } ?>
			
				<?php if( $view->class =="clientcourseView"){  ?>	
						<th></th>
				<?php } ?>	

			</tr>
			<?php } ?>
		</thead>
	
		<?php if( $view->pagination > 0){ ?>
			<tbody data-bind="foreach: views.<?php echo $view->class;?>().viewport">
		<?php }else{ ?>
			<tbody data-bind="foreach: views.<?php echo $view->class;?>().model.body">
		<?php } ?>
	

			<tr>
			   <!-- ko ifnot: state.edit-->
			   	<td class="center minpadding" data-bind="text:row_num, click:events.clickCell"></td>
			   	
			   	<td class="minpadding edit" data-bind="click:events.clickCell" colspan="3"><span class="glyphicon glyphicon-pencil"></span> </td>
			   	

				<?php foreach ($view->header as $key => $value) { ?>
					
				<?php	if ($value["format"] == "c") { 
						if($simple){
							$value["place"] = "main";
						};
					 	if($simple || $value["place"]=="main"){	

					?>

						<td class ="minpadding squareTableTD" data-bind="squareTable:{count:items().<?php echo $value["fields"][0] ;?> , total:items().<?php echo $value["fields"][1] ;?>}"></td>


				<?php	
						}
					}else{ 
						if($simple){$value["place"] = "main";};
					 	if($simple || $value["place"]=="main"){
						?>
						<td class="center minpadding" data-bind="text:items().<?php echo $key; ?>"></td>
				<?php	}	
					}
				?>			


				<?php  } ?>	
				<!-- /ko -->
				<!-- ko if: state.edit-->
				<td data-bind="text:row_num,click:events.clickCell"></td>
			   	<td class="edit minpadding" data-bind="click:events.clickCell"><span class="glyphicon glyphicon-share-alt"></span> </td>
				<td class="minpadding"> <span class="glyphicon glyphicon-ok" data-bind="click:events.clickCellSave" ></span> </td>
				<td class="minpadding"><span class="glyphicon glyphicon-remove" data-bind="click:events.clickCellDelete"></span></td>
				
				<?php foreach ($view->header as $key => $value) { 
					if( $value["format"] == "s" || $value["format"] == "d"){
					 	if($simple){$value["place"] = "main";};
					 	if($simple || $value["place"]=="main"){
				?>
							<td> <input type="text" placeholder="<?php echo $value["name"]; ?>" data-bind="value:items().<?php echo $key; ?>"></td>
				<?php   
						}
					} elseif ($value["format"] == "select") {
						if($simple){$value["place"] = "main";};
					 	if($simple || $value["place"]=="main"){
				?>			
						<td> 
								<select data-bind="options:selects.<?php echo $value["select_name"];?>.values, optionsText:'field', value:selects.<?php echo $value["select_name"];?>.value, event:{change:events.selected}  "></select>
						</td>					
				<?php   
						}
					} elseif ($value["format"] == "date") {
						if($simple){$value["place"] = "main";};
					 	if($simple || $value["place"]=="main"){
				?>			
							<td> 
								<input placeholder="<?php echo $value["name"]; ?>"  class='pickadate' data-bind="datepicker:items().<?php echo $key; ?>">
							</td>						
				<?php   
						}
					} elseif ($value["format"] == "t") {
						if($simple){$value["place"] = "main";};
					 	if($simple || $value["place"]=="main"){
				?>			
							<td> 
								<textarea placeholder="<?php echo $value["name"]; ?>" cols="30" rows="3" data-bind="value:items().<?php echo $key; ?>"></textarea>		
							</td>						
				<?php   
						}	
					} elseif ($value["format"] == "c") {
						if($simple){$value["place"] = "main";};
					 	if($simple || $value["place"]=="main"){
				?>			
							<td class="minpadding squareTableTD" data-bind="squareTable:{count:items().<?php echo $value["fields"][0] ;?> , total:items().<?php echo $value["fields"][1] ;?>, edit:state.edit}"></td>							
				<?php	}
					}
				} 
				?>	
				<!-- /ko -->

				<?php if( $view->class =="clientcourseView"){  ?>	
						<td data-bind="if:items().needpay_visible"> 
						   <button class="tablePageButton" data-bind="click:events.pay" > Оплатить </button> 
						</td>
				<?php } ?>	
			</tr>		
			


			
			<?php if(!$simple){ ?>
			<tr class="addrow">
			   <!-- ko ifnot: state.edit-->
			   	
			   	
			   	<td colspan="4"></td>
			   	

				<?php foreach ($view->header as $key => $value) { ?>
					
				<?php	if ($value["format"] == "c") { 
						if($value["place"]=="add"){

					?>

						<td class ="minpadding squareTableTD" data-bind="squareTable:{count:items().<?php echo $value["fields"][0] ;?> , total:items().<?php echo $value["fields"][1] ;?> ,edit:state.edit}"></td>


				<?php	
						}
					}else{ 
						if($value["place"]=="add"){
						?>
						<td class="center minpadding" data-bind="text:items().<?php echo $key; ?>"></td>
				<?php	}	
					}
				?>			

				<?php  } ?>	
				<?php if($count_td_main - $count_add > 0){ ?>
					<td colspan="<?php echo ($count_td_main - $count_add); ?>"></td>
				<?php } ?>

				<!-- /ko -->
				<!-- ko if: state.edit-->
				
				<td colspan="4"></td>
				
				<?php foreach ($view->header as $key => $value) { 
					if( $value["format"] == "s" || $value["format"] == "d"){
					 	if($value["place"]=="add"){
				?>
							<td class="minpadding"> <input type="text" placeholder="<?php echo $value["name"]; ?>" data-bind="value:items().<?php echo $key; ?>"></td>
				<?php   
						}
					} elseif ($value["format"] == "t") {
						if($value["place"]=="add"){
				?>			
						<td class="minpadding"> 
								<textarea  placeholder="<?php echo $value["name"]; ?>" cols="30" rows="3" data-bind="value:items().<?php echo $key; ?>"></textarea>			
						</td>					
				<?php   
						}	
					} elseif ($value["format"] == "select") {
						if($value["place"]=="add"){
				?>			
						<td class="minpadding"> 
								<select data-bind="options:selects.<?php echo $value["select_name"];?>.values, optionsText:'field', value:selects.<?php echo $value["select_name"];?>.value, event:{change:events.selected}  "></select>
						</td>					
				<?php   
						}
					} elseif ($value["format"] == "date") {
						if($value["place"]=="add"){
				?>			
							<td class="minpadding"> 
								<input placeholder="<?php echo $value["name"]; ?>" class='pickadate' class='pickadate' data-bind="datepicker:items().<?php echo $key; ?>"></select>
							</td>						
				<?php   
						}
					} elseif ($value["format"] == "c") {
						if($value["place"]=="add"){
				?>			
							<td class="squareTableTD" data-bind="squareTable:{count:items().<?php echo $value["fields"][0] ;?> , total:items().<?php echo $value["fields"][1] ;?>}"></td>							
				<?php	}
					}
				} 
				?>	
				<?php if($count_td_main - $count_add > 0){ ?>
					<td class="minpadding" colspan="<?php echo ($count_td_main - $count_add); ?>"></td>
				<?php } ?>
				<!-- /ko -->
				<?php if( $view->class =="clientcourseView"){  ?>	
						<td>  </td>
				<?php } ?>	
			</tr>					
	
			<?php } ?>
		</tbody>
	</table>
		<?php if( $view->pagination > 0){ ?>
			<div>  

				<button class="tablePageButton" data-bind="click:views.<?php echo $view->class;?>().clickBackward"> < </button> <span data-bind='text:views.<?php echo $view->class;?>().viewportSetting().curPage'></span> of <span data-bind='text:views.<?php echo $view->class;?>().viewportSetting().pageCount'></span> <button class="tablePageButton" data-bind="click:views.<?php echo $view->class;?>().clickForward"> > </button>
			 </div>
		<?php } ?>	
</div>

<hr>
<script>

			jQuery(function(){
				var supLoc1 = function(){
					try {
					    return 'localStorage' in window && window['localStorage'] !== null;
					} catch (e) {
					    return false;
					}
				}

				window.supportLocalStorage = window.supportLocalStorage	|| supLoc1;

				var changeLocalStorage = function(key){
		    		if( supportLocalStorage() ){
		    			var varname = "timetable_" + key;
		    			localStorage[varname] = 1 - localStorage[varname];
		    		}
		    	}


				jQuery("#<?php echo $view->class; ?>_view_table_click").click(function(){

				    var main = "<?php echo $view->class; ?>_view_table";
					var button = "<?php echo $view->class; ?>_view_table_v";

			    	var butText = jQuery("#"+button).text();
					if(butText == " >> "){
						butText = " V ";
					}else{
						butText = " >> ";
					}
					jQuery("#"+button).text(butText);
					jQuery("#"+main).toggle();
					changeLocalStorage(main);


				});

				if( supportLocalStorage() ){
				    var main = "<?php echo $view->class; ?>_view_table";
					var button = "<?php echo $view->class; ?>_view_table_v";

					var key = "timetable_" + main;
					var val = localStorage[key];

					if(val === undefined){
						val = 0;
						localStorage[key] = val;
					}

					if(val == 1){
						$("#"+main).toggle();	
						$("#"+button).text(" V ");
					}

				}
			});


	var tmp_string = <?php  echo json_encode( $view->getView() ); ?>;
	tmp_string.pagintaion = <?php  echo  $view->pagination; ?>;
	var _DATAVIEWS = _DATAVIEWS || {};
	_DATAVIEWS['<?php echo $view->class; ?>'] = tmp_string;
	//console.log(_DATAVIEWS);
</script>


<!-- choose user -->
<div id="clientCourseViewUsers" >
	<div class="exists">
		<button data-bind="click:choiceDiv.newUser" >Новый</button>
		<button data-bind="click:choiceDiv.cancel" >Отмена</button>
		<ul data-bind="foreach:clientSearchResults">
			<li data-bind="click:click">
				<span data-bind="text:client_name"></span><br>
				<span data-bind="text:client_contact"></span><br>

			</li>
		</ul>
	</div>
	<div class="new">
		<br>
		<button data-bind="click:choiceDiv.cancel" >Отмена</button>
		<fieldset>
			<!--form id="register_form" name="register_form" method="POST" action="" data-bind="submit:choiceDiv.clickReg"-->
				<input type="text" data-bind="value:choiceDiv.form.client_name" name="client_name" required placeholder="Логин"><br> 
				<input type="text" data-bind="value:choiceDiv.form.client_fullname" name="client_fullname" required placeholder="Имя пользователя"><br> 
				<input type="text" data-bind="value:choiceDiv.form.client_email" name="client_email" required placeholder="e-mail"><br> 
				<input type="hidden" data-bind="value:choiceDiv.form.role" name="role" value="5"><br>
				<input type="password" data-bind="value:choiceDiv.form.client_password" name="client_password" required placeholder="пароль"><br> 
				<input type="password" data-bind="value:choiceDiv.form.client_password2" name="client_password2" required placeholder="повтор пароля"><br> 
				<button data-bind="click:choiceDiv.clickReg">Регистрировать</button>
			<!--/form-->
		</fieldset>
	</div>
</div>

<div id="paymentDivModalbg">
	<div id="paymentDivModal">
		
	</div>
</div>

<?php }?>